package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.GTnameService;
import com.hhs.storage.service.UserService;
import com.hhs.storage.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2020-06-15
 */
@RestController
@RequestMapping("/do/gtname")
@Slf4j
public class GTnameController {
    @Resource
    GTnameService gTnameService;

    @RequestMapping(value = "getAll", method = RequestMethod.GET)
    public Map<String, Object> getAll(InputParam inputParam) {
        log.info("查询名字分页信息");
        EntityWrapper ew = new EntityWrapper<GTname>();
        ew.orderBy("sx_");
        Page<GTname> result = gTnameService.selectPage(new Page<GTname>(inputParam.getPage(), inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }

    @RequestMapping(value = "edit")
    public Map<String, Object> saveOrUpdate(GTname gtname){
        log.info("编辑名字信息");
        gTnameService.updateById(gtname);
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "getnames")
    public Map<String, Object> getnames(){
        log.info("得到名字信息");
        EntityWrapper ew = new EntityWrapper<GTname>();
        ew.orderBy("id_");
        List<GTname> list = gTnameService.selectList(ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", list);
        return map;
    }

    @RequestMapping(value = "/uploadpic"
            , method = RequestMethod.POST
            , produces = "application/json; charset=utf-8")
    @ResponseBody
    public Map<String, Object> uploadpic(@RequestParam() MultipartFile file) {
        //在这里面文件存储的方案一般是：收到文件→获取文件名→在本地存储目录建立防重名文件→写入文件→返回成功信息
        //如果上面的步骤中在结束前任意一步失败，那就直接失败了。
        Map responseObj=Result.failJsonResp("null");
        String path="";
        FileOutputStream out=null;
        if (null == file || file.isEmpty()) {
            responseObj=Result.failJsonResp("文件不能为空！");
        }else{
            responseObj=Result.sucJsonResp("文件上传成功");
            //创建一个文件夹，网上代码很多
            String url = FileUtils.mkdirs("/upload/");
            try {
                //获得二进制流并输出
                byte[] f = file.getBytes();
                path=url+file.getOriginalFilename();
                out = new FileOutputStream(path);
                out.write(f);

            } catch (IOException e) {
                responseObj=Result.failJsonResp("文件上传失败！");
            }finally {
                // 完毕，关闭所有链接
                try {
                    out.close();
                } catch (IOException e) {
                    log.info("关闭流失败");
                }
            }

        }
        //更新数据库
        GTname gTname=new GTname();
        gTname.setId(1l);
        gTname.setName(path);
        gTnameService.updateById(gTname);
        return responseObj;
    }
}

